package com.analyst.service.impl;

import com.analyst.entity.EtlDependency;
import com.analyst.entity.EtlJob;
import com.analyst.entity.Job;
import com.analyst.mapper.EtlDependencyMapper;
import com.analyst.mapper.EtlJobMapper;
import com.analyst.service.IDependService;
import com.analyst.utils.VirtualJobHelper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class dependService extends ServiceImpl<EtlDependencyMapper, EtlDependency> implements IDependService {
    @Resource
    private EtlDependencyMapper dependencyMapper;
    @Resource
    private EtlService etlService;

    @Resource
    private EtlJobMapper etlJobMapper;

    @Resource
    private EtlDependencyMapper etlDependencyMapper;

    @Override
    public List<Job> getJobListByPage() {
        HashMap<String, Job> map = new HashMap<>();
        List<Job> jobs = new ArrayList<>();
        int currentPage = 1;
        // 每页的数据条数，可以根据需要调整
        int pageSize = 5;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        while (true) {
            List<EtlJob> list = getJobListByPage(currentPage, pageSize);
            if (list.isEmpty()) {
                // 所有数据已获取
                break;
            }

            // 处理 list 数据并添加到 jobs 中
            for (EtlJob etlJob : list) {
                String etl_system = etlJob.getEtlSystem();
                String etl_job = etlJob.getEtlJob();

                String dateString = sdf.format(etlJob.getTxdate());
                String id = etl_system + "_" + etl_job + "_" + dateString;

                Job sqlJob = new Job();
                sqlJob.setId(id);
                sqlJob.setBeginTime(etlJob.getStarttime());
                sqlJob.setEndTime(etlJob.getEndtime());

                // 将 sqlJob 添加到 jobs 列表中
                jobs.add(sqlJob);
                map.put(id, sqlJob);
            }

            for (EtlJob etlJob : list) {
                String etl_system = etlJob.getEtlSystem();
                String etl_job = etlJob.getEtlJob();

                String dateString = sdf.format(etlJob.getTxdate());
                String key1 = etl_system + "_" + etl_job + "_" + dateString;

                Job sqlJob = map.get(key1);

                List<EtlDependency> depList = query().eq("etl_system", etl_system).eq("etl_job", etl_job).list();
                for (EtlDependency dependency : depList) {
                    List<EtlJob> list1 = etlService.query().eq("etl_system", dependency.getDependencySystem()).eq("etl_job", dependency.getDependencyJob()).list();

                    for (EtlJob job : list1) {
                        dateString = sdf.format(job.getTxdate());
                        String key = job.getEtlSystem() + "_" + job.getEtlJob() + "_" + dateString;
                        Job depJob = map.get(key);
                        // 依赖
                        sqlJob.getDependencies().offer(depJob);
                        depJob.getNextList().add(sqlJob);
                    }
                }
            }
            currentPage++;
        }
        return jobs;
    }


    @Override
    public List<Job> getJobList(){
        List<EtlJob> etlJobs = etlJobMapper.getAllEtlJobs();
        Map<String, Job> jobMap = new HashMap<>();
        List<Job> jobs = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (EtlJob etlJob : etlJobs) {
            String etl_system = etlJob.getEtlSystem();
            String etl_job = etlJob.getEtlJob();

            String dateString = sdf.format(etlJob.getTxdate());

            String id = etl_system + "_" + etl_job + "_" + dateString;

            Job sqlJob = new Job();
            sqlJob.setId(id);
            sqlJob.setBeginTime(etlJob.getStarttime());
            sqlJob.setEndTime(etlJob.getEndtime());

            jobs.add(sqlJob);
            jobMap.put(id,sqlJob);
        }


        for (EtlJob etlJob : etlJobs) {

            String etl_system = etlJob.getEtlSystem();
            String etl_job = etlJob.getEtlJob();

            String dateString = sdf.format(etlJob.getTxdate());
            String key1 = etl_system + "_" + etl_job + "_" + dateString ;
            Job sqlJob = jobMap.get(key1);


            List<EtlDependency> depList = etlDependencyMapper.getDependenciesByEtlJob(etl_system, etl_job);
            for (EtlDependency dependency : depList) {
                List<EtlJob> dependencyJobs = etlJobMapper.getEtlJobsByEtlSystemAndJob(dependency.getDependencySystem(), dependency.getDependencyJob());
                for (EtlJob job : dependencyJobs) {
                    dateString = sdf.format(job.getTxdate());
                    String key = job.getEtlSystem() + "_" + job.getEtlJob() + "_" + dateString ;
                    Job depJob = jobMap.get(key);
                    //依赖
                    sqlJob.getDependencies().offer(depJob);
                    depJob.getNextList().add(sqlJob);
                }

            }

        }
        return jobs;
    }

    /**
     * 从数据库中读取数据并存入Job对象中，同时筛选掉虚节点，不予存储
     * 对于本地自己设计的简单数据，会删掉一些节点，比如A.a和B.a、C.a，只会留下耗时最长的A.a
     *
     * @return
     */
//    @Override
//    public List<Job> getJobList() {
////        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        //用于暂时存储jobname和Job对象的映射，用于设置job对象的依赖节点集合和后继节点集合
//        HashMap<String, Job> jobMap = new HashMap<>();
//        //最后需要返回的jobs，里面的job对象都被设置好开始结束时间以及依赖节点集合和后继节点集合
//        List<Job> jobs = new ArrayList<>();
//        //记录虚节点替换为其依赖节点的map，如虚节点A.a,B.a依赖于C.a，那么map中就记录:A.a->C.a、B.a->C.a
//        Map<String, String> replaceHistory = new HashMap<>();
//        // 映射为: etlsystem+etljob -> EtJob
//        Map<String, EtlJob> etlJobMapAfterFilter = new HashMap<>();
//        // 获取etl_job表中所有记录
//        List<EtlJob> etlJobs = etlJobMapper.getAllEtlJobs();
//        // 去类似于F-SPCR-110110_M03_SPP-PSCP-STHPAMBK-S_030的jobname中的F-SPCR-110110 得到M03SPP-P_SCPSTHPAMBK-S_030
//        for (EtlJob etlJob : etlJobs) {
//            String etl_system = etlJob.getEtlSystem();
//            String etl_job = VirtualJobHelper.divideVirtualJobName(etl_system, etlJob.getEtlJob())[1];
//            etlJob.setEtlJob(etl_job);
//        }
//        // 得到etlJobs对应的map,key是etljob， value是等于某个etljob的EtLjob对象集合,且这些EtLJob对象按照endtime-starttime以小到大排别
//        Map<String, List<EtlJob>> etlJobMap = etlJobs.stream().collect(
//                Collectors.groupingBy(
//                        EtlJob::getEtlJob,
//                        Collectors.mapping(
//                                e -> e,
//                                Collectors.collectingAndThen(
//                                        Collectors.toList(),
//                                        // 按照endtime-stanttime从小到大排序
//                                        cur -> cur.stream().sorted(Comparator.comparingLong(
//                                                e -> e.getEndtime().getTime() - e.getStarttime().getTime()
//                                        )).collect(Collectors.toList())
//                                )
//                        )
//                )
//        );
//        // 根据获取到的etlJob数据，设置job对象的ID、begintime、endtime
//        for (String key : etlJobMap.keySet()) {
//            // 获取每个key所对应的EtlJob的List，这个List中，所有EtLJob的iobname都是key
//            List<EtlJob> curList = etlJobMap.get(key);
//            if (curList.size() == 1) {
//                EtlJob curMax = curList.get(0);
//            }
//            // 反转，得到endtime-starttime从大到小排序的curList
//            Collections.reverse(curList);
//            EtlJob curMax = curList.get(0);
//            // 把替换记录加入replaceHistory中，curList按照耗时从大到小排序，replacehistory中放入的则是curList的第一个元素之后的元素分别和第一个元素组成的组
//            // 如curList=[A.a,B.a,C.a],那么replaceHistory={B.a->A.a,C.a->A.a}
//            Map<String, String> curReplace = curList.stream().skip(1).collect(Collectors.toMap(
//                    e -> e.getEtlSystem() + e.getEtlJob(),
//                    e -> curMax.getEtlSystem() + curMax.getEtlJob(),
//                    (oldValue, newValue) -> newValue
//            ));
//            replaceHistory.putAll(curReplace);
//            // 根据curMax，设置job对象的ID、begintime、endtime
//            String etl_system = curMax.getEtlSystem();
//            String etl_job = curMax.getEtlJob();
//            String id = etl_system + "." + etl_job;
//            String etlSystemAndJob = etl_system + etl_job;
//            etlJobMapAfterFilter.put(etlSystemAndJob, curMax);
//
//            Job sqlJob = new Job();
//            sqlJob.setId(id);
//            sqlJob.setBeginTime(curMax.getStarttime());
//            sqlJob.setEndTime(curMax.getEndtime());
//
//            jobs.add(sqlJob);
//            jobMap.put(id, sqlJob);
//        }
//
//
//        for (String jobName : etlJobMap.keySet()) {
//            List<EtlJob> curList = etlJobMap.get(jobName);
//            // 反转curList，只需取endtime-starttime最大的EtJob
//            Collections.reverse(curList);
//            EtlJob etlJob = curList.get(0);
//            String etl_system = etlJob.getEtlSystem();
//            String etl_job = etlJob.getEtlJob();
//            String key1 = etl_system + "." + etl_job;
//            Job sqlJob = jobMap.get(key1);
//            //job的依赖集合
//            List<EtlDependency> depList = etlDependencyMapper.getDependenciesByEtlJob(etl_system, etl_job);
//            List<EtlJob> dependencyJobs = new ArrayList<>();
//            for (EtlDependency dependency : depList) {
//                String dependencySystem = dependency.getDependencySystem();
//                String dependencyJob = dependency.getDependencyJob();
//                dependencyJob = VirtualJobHelper.divideVirtualJobName(dependencySystem, dependencyJob)[1];
//                String depSystemAndJob = dependencySystem + dependencyJob;
//                if (dependencySystem.length() == 3 && dependencySystem.startsWith("Z")) {
//                    depSystemAndJob = "IMP" + dependencyJob;
//                }
//                if (replaceHistory.containsKey(depSystemAndJob))
//                    depSystemAndJob = replaceHistory.get(depSystemAndJob);
//
//                EtlJob toAdd = etlJobMapAfterFilter.get(depSystemAndJob);
//                dependencyJobs.add(toAdd);
//                for (EtlJob job : dependencyJobs) {
//                    String key = job.getEtlSystem() + "." + job.getEtlJob();
//                    Job depJob = jobMap.get(key);
//                    sqlJob.getDependencies().offer(depJob);
//                    depJob.getNextList().add(sqlJob);
//                }
//            }
//        }
//        return jobs;
//    }

    @Override
    public List<EtlJob> getJobListByPage(int page, int pageSize) {
        Page<EtlJob> etlJobPage = new Page<>(page, pageSize);
        // 获取总记录数
        int total = (int) etlService.count();
        // 设置总记录数
        etlJobPage.setTotal(total);
        return etlService.page(etlJobPage).getRecords();
    }


    public static void main(String[] args) {


    }
}
